home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8642
/
8642.xpi
/
chrome
/
bablatoolbar.jar
/
content
/
bablatoolbar.js
next >
Wrap
Text File
|
2009-10-29
|
8KB
|
184 lines
// The babla_Search() function will perform a Google search for us. The two
// parameters that get passed in are the event that triggered this function
// call, and the type of search to perform.
////////////////////////////////////////////////////////////////////////////////
function babla_Search(event, type)
{
// This variable will hold the URL we will browse to
var URL = "";
// This variable will tell us whether our search box is empty or not
var isEmpty = false;
// Get a handle to our search terms box (the <menulist> element)
var searchTermsBox = document.getElementById("babla-SearchTerms");
var searchGoogleTermsBox = document.getElementById("google-SearchTerms");
var searchWikiTermsBox = document.getElementById("wiki-SearchTerms");
// Get the value in the search terms box, trimming whitespace as necessary
// See the babla_TrimString() function farther down in this file for details
// on how it works.
var searchbablaTerms = babla_TrimString(searchTermsBox.value);
var searchGoogleTerms = babla_TrimString(searchGoogleTermsBox.value);
var searchWikiTerms = babla_TrimString(searchWikiTermsBox.value);
if(searchbablaTerms.length == 0) // Is the search terms box empty?
isEmpty = true; // If so, set the isEmpty flag to true
else // If not, convert the terms to a URL-safe string
searchbablaTerms = babla_ConvertTermsToURI(searchbablaTerms);
// Now switch on whatever the incoming type value is
// If the search box is empty, we simply redirect the user to the appropriate
// place at the Google website. Otherwise, we search for whatever they entered.
switch(type)
{
// Build up the URL for a web search
case "babla":
var ddl = document.getElementById("bablaLanguage").value;
var dictionary = document.getElementById("dictionary").value;
default:
var sitelang = document.getElementById("sitelang").value;
if(isEmpty) { URL = "http://" +sitelang+ ".bab.la/"; }
else { URL = "http://" +sitelang+ ".bab.la/" +dictionary+ "/"+ddl+ "/" + searchbablaTerms + ".html";}
break;
// Build up the URL for a wiki search
case "google":
googleUrl = document.getElementById("googleUrl").value;
URL = "http://" +googleUrl+ "/search?q=" + searchGoogleTerms;
break;
// Build up the URL for a wiki search
case "wiki":
var wikilang = document.getElementById("wikilang").value;
URL = "http://" +wikilang+ ".wikipedia.org/wiki/Special:Search?search=" + searchWikiTerms;
break;
}
// Load the URL in the browser window using the babla_LoadURL function
babla_LoadURL(URL);
}
////////////////////////////////////////////////////////////////////////////////
// The babla_TrimString() function will trim all leading and trailing whitespace
// from the incoming string, and convert all runs of more than one whitespace
// character into a single space. The altered string gets returned.
////////////////////////////////////////////////////////////////////////////////
function babla_TrimString(string)
{
// If the incoming string is invalid, or nothing was passed in, return empty
if (!string)
return "";
string = string.replace(/^\s+/, ''); // Remove leading whitespace
string = string.replace(/\s+$/, ''); // Remove trailing whitespace
// Replace all whitespace runs with a single space
string = string.replace(/\s+/g, ' ');
return string; // Return the altered value
}
////////////////////////////////////////////////////////////////////////////////
// The babla_ConvertTermsToURI() function converts an incoming string of search
// terms to a safe value for passing into a URL.
////////////////////////////////////////////////////////////////////////////////
function babla_ConvertTermsToURI(terms)
{
// Create an array to hold each search term
var termArray = new Array();
// Split up the search term string based on the space character
termArray = terms.split(" ");
// Create a variable to hold our resulting URI-safe value
var result = "";
// Loop through the search terms
for(var i=0; i<termArray.length; i++) {
// All search terms (after the first one) are to be separated with a '+'
if(i > 0)
result += "+";
// Encode each search term, using the built-in Firefox function
// encodeURIComponent().
result += encodeURIComponent(termArray[i]);
}
return result; // Return the result
}
////////////////////////////////////////////////////////////////////////////////
// The babla_LoadURL() function loads the specified URL in the browser.
////////////////////////////////////////////////////////////////////////////////
function babla_LoadURL(url)
{
// Set the browser window's location to the incoming URL
//window._content.document.location = url;
//Open a new tab to load the url
getBrowser().selectedTab = getBrowser().addTab(url);
// Make sure that we get the focus
// window.content.focus();
}
////////////////////////////////////////////////////////////////////////////////
// The babla_KeyHandler() function checks to see if the key that was pressed
// is the [Enter] key. If it is, a web search is performed.
////////////////////////////////////////////////////////////////////////////////
/*function babla_KeyHandler(event)
{
// Was the key that was pressed [ENTER]? If so, perform a web search.
if(event.keyCode == event.DOM_VK_RETURN)
babla_Search(event, 'web');
}
****/
function babla_KeyHandler(event, type1)
{
// Was the key that was pressed [ENTER]? If so, perform a web search.
if(event.keyCode == event.DOM_VK_RETURN)
switch(type1) {
// Build up the URL for a web search
case "babla":
default:
babla_Search(event, 'babla');
break;
// Build up the URL for a wiki search
case "google":
babla_Search(event, 'google');
break;
// Build up the URL for a wiki search
case "wiki":
babla_Search(event, 'wiki');
break;
}
}
////////////////////////////////////////////////////////////////////////////////
// The babla_loadPref() function checks for settings of the dictionary selection
////////////////////////////////////////////////////////////////////////////////
function babla_loadPref() {
const babla_PrefService = Components.classes["@mozilla.org/preferences-service;1"]. getService(Components.interfaces.nsIPrefService);
const babla_Branch = babla_PrefService.getBranch("bablatoolbar.");
if (babla_Branch.prefHasUserValue("udict.selected")) {
var selDict = babla_Branch.getCharPref("udict.selected");
document.getElementById('bablaLanguage').selectedIndex = selDict;
}
else {
babla_Branch.setUnicharPref("udict.selected", document.getElementById("bablaLanguage").value);
}
}
window.addEventListener("load", babla_loadPref, false);
////////////////////////////////////////////////////////////////////////////////
// The babla_savePref() function sets the dictionary selection to user prefs
////////////////////////////////////////////////////////////////////////////////
function babla_savePref() {
const babla_PrefService = Components.classes["@mozilla.org/preferences-service;1"]. getService(Components.interfaces.nsIPrefService);
const babla_Branch = babla_PrefService.getBranch("bablatoolbar.");
// babla_Branch.setCharPref("udict.selected", document.getElementById("bablaLanguage").value);
var selObj = document.getElementById("bablaLanguage");
var selIndex = selObj.selectedIndex;
babla_Branch.setCharPref("udict.selected", selIndex);
}